package com.Object;

import java.util.Random;

import javax.microedition.khronos.opengles.GL10;

import android.graphics.Bitmap.Config;

import com.Core.GameEngine;
import com.Object.AllRoute.GhostShip;
import com.Object.AllRoute.NullObject;
import com.Object.AllRoute.Pearl;
import com.Object.AllRoute.Rock;
import com.Object.AllRoute.Shell;
import com.Object.AllRoute.Tornado;
import com.Object.Route1.BigFish;
import com.Object.Route1.Corail;
import com.Object.Route1.GiantFish;
import com.Object.Route1.MediumFish;
import com.Object.Route1.Mermaid;
import com.Object.Route1.SmallFish;
import com.Object.Route2.Poseidon;
import com.Object.Route2.Ship;
import com.Object.Route2.Shipwreck;
import com.Object.Route2.Treasure;
import com.Object.Route2.TreasureMap;

public class SeaObject
{	protected ObjType type;
	protected int[] frameSlop = {};
	protected int[][] texPos = {};
	protected int frameCount=0, slopTrack=0;
	protected boolean scaleAni = false;
	protected ObjType parent;
	protected int score;
	
	public static SeaObject CreateObject(ObjType type)
	{	switch (type)
		{	case Rock		:	return new Rock();
			case Tornado	:	return new Tornado();
			case GhostShip	:	return new GhostShip();
			case Pearl		:	return new Pearl();
			case Shell		:	return new Shell();
			case Corail		:	return new Corail();
			case SmallFish	:	return new SmallFish();
			case MediumFish	:	return new MediumFish();
			case BigFish	:	return new BigFish();
			case GiantFish	:	return new GiantFish();
			case Mermaid	:	return new Mermaid();
			case Ship		:	return new Ship();
			case Shipwreck	:	return new Shipwreck();
			case Treasure	:	return new Treasure();
			case TreasureMap:	return new TreasureMap();
			case Poseidon	:	return new Poseidon();
			default			:	return new NullObject();
		}
	}
	public void DrawAtCell(GL10 gl, int row, int col)
	{	if (texPos.length>0)
		{	if (frameSlop.length>=2)
			{	frameCount++;
				if (frameCount == frameSlop[slopTrack])
				{	slopTrack = (slopTrack+1)%frameSlop.length;
					frameCount = 0;
				}
			}
			GameEngine.texLoader.DrawAtCell(gl, row, col, texPos[slopTrack][0], texPos[slopTrack][1], scaleAni);
		}
	}
	public void DrawAtPos(GL10 gl, float x, float y, float scaleRatio)
	{	if (texPos.length>0)
		{	if (frameSlop.length>=2)
			{	frameCount++;
				if (frameCount == frameSlop[slopTrack])
				{	slopTrack = (slopTrack+1)%frameSlop.length;
					frameCount = 0;
				}
			}
			GameEngine.texLoader.DrawAtPos(gl, x, y, texPos[slopTrack][0], texPos[slopTrack][1], scaleRatio);
		}
	}
	@Override
	public String toString()
	{	return getType().toString();
	}
	public ObjType getType()
	{	return this.type;
	}
	public void setType(ObjType type)
	{	this.type = type;
	}
	public boolean isScaleAni()
	{	return scaleAni;
	}
	public void SetScaleAni()
	{	scaleAni=true;
	}
	public void RemoveScaleAni()
	{	scaleAni=false;
	}
	public ObjType getParent() {
		return parent;
	}
	public int getScore() {
		return score;
	}
	public static ObjType randomObject() {
		int [] id = GameEngine.curMap.getObjID();
		int [] percent = GameEngine.curMap.getObjPercent();
		
		Random ran = new Random();
		int ranPercent = ran.nextInt(100);
		for(int x = 0; x < ranPercent; x++){
			ran.nextInt(100);
		}
		ranPercent = ran.nextInt(100);
		int i = 0;
		int curPercent = percent[i];
		while(ranPercent > curPercent && i < id.length){
			i++;
			curPercent += percent[i];
		}			
		return convertIdToObjectType(id[i]);
	}
	public static ObjType convertIdToObjectType(int id) {
		switch (id)
		{	case GameEngine.ROCK		:	return ObjType.Rock;
			case GameEngine.TORNADO		:	return ObjType.Tornado;
			case GameEngine.GHOST_SHIP	:	return ObjType.GhostShip;
			case GameEngine.PEARL		:	return ObjType.Pearl;
			case GameEngine.SHELL		:	return ObjType.Shell;
			case GameEngine.CORAIL		:	return ObjType.Corail;
			case GameEngine.SMALL_FISH	:	return ObjType.SmallFish;
			case GameEngine.MEDIUM_FISH	:	return ObjType.MediumFish;
			case GameEngine.BIG_FISH	:	return ObjType.BigFish;
			case GameEngine.GIANT_FISH	:	return ObjType.GiantFish;
			case GameEngine.MERMAID		:	return ObjType.Mermaid;
			case GameEngine.SHIP		:	return ObjType.Ship;
			case GameEngine.SHIP_WRECK	:	return ObjType.Shipwreck;
			case GameEngine.TREASURE	:	return ObjType.Treasure;
			case GameEngine.TREASURE_MAP:	return ObjType.TreasureMap;
			case GameEngine.POSEIDON 	:	return ObjType.Poseidon;
			default 					:	return null;
		}
	}
	public static int convertObjTypeToId(ObjType tp)
	{	switch (tp)
		{	case Rock		:	return GameEngine.ROCK;
			case Tornado	:	return GameEngine.TORNADO;
			case GhostShip	:	return GameEngine.GHOST_SHIP;
			case Pearl		:	return GameEngine.PEARL;
			case Shell		:	return GameEngine.SHELL;
			case Corail		:	return GameEngine.CORAIL;
			case SmallFish	:	return GameEngine.SMALL_FISH;
			case MediumFish	:	return GameEngine.MEDIUM_FISH;
			case BigFish	:	return GameEngine.BIG_FISH;
			case GiantFish	:	return GameEngine.GIANT_FISH;
			case Mermaid	:	return GameEngine.MERMAID;
			case Ship		:	return GameEngine.SHIP;
			case Shipwreck	:	return GameEngine.SHIP_WRECK;
			case Treasure	:	return GameEngine.TREASURE;
			case TreasureMap:	return GameEngine.TREASURE_MAP;
			case Poseidon	:	return GameEngine.POSEIDON;
			default			:	return GameEngine.NULL; 
		}
	}
}
